<?php

namespace app\api\controller;
use think\facade\Db;
use api\model\Factory;
class Exec
{
    //设备类型
    private $device = '';

    /**
     * 执行单个sevice服务
     * 请求路径 http://local.com/api/v1/basic/notice/info?id=4
     * api/版本/服务分类/模块/方法?参数名=参数值&参数名=参数值
     * @return 执行结果
     *{
     *    "status": 1,
     *    "data": {}
     *}
     */
    public function exec($service, $method)
    {

        //参数
        $args = input(false); //不过滤，防新guid被过滤

        //移除公共的路由参数
        unset($args['service'],$args['method'],$args['s']);

        $sys_close = tpCache('shop_info.sys_close');
        $close_reason = tpCache('shop_info.sys_close_memo');

        $err = '';
        if($sys_close){
            $err = $close_reason?:'站点关闭';
        }
        if($err != '') {
            //apilog 验证失败
            $rt = ['status' => 0, 'msg' => $err];
            $this->apiLog(0,$rt);

            return json($rt);
        }
        //业务处理
        $platform = input('platform');
        if($platform){
            define('platform',$platform);
        }else{
            if(!is_weixin()){
                define('platform','h5');
            }else{
                define('platform','mp');
            }
        }
        $return = service($service.'/'.$method, input());
        writeLog('返回内容'.json_encode($return),'filesync');
        //apilog 成功
        $rs_api = $return;
        if(!is_array($return)){
            return $return;
        }
        $this->apiLog($return['status'],$return);

        return $this->result_json($return);
    }
    public function apilog($status,$result){
        //数据
        $args = input(false); //不过滤，防新guid被过滤
        //移除公共的路由参数
        unset($args['service'],$args['method'],$args['s']);
        $arg = $args;
        $arg = json_encode($arg, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_NUMERIC_CHECK);
        if(strlen($arg) > 60000) {
            $arg = '数据太多，不记录';
        }
        //请求数据
        $data_log = [
            "api" => input('service').'/'.input('method'),
            'arg' => json_encode($arg, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_NUMERIC_CHECK),
            "ip" => request()->ip(),
            "w_time" => time(),
            'headers' => json_encode(request()->header(), JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_NUMERIC_CHECK),
        ];
        //当前用户Id

        if(!empty($args['token'])) {
            $user = json_decode(base64_decode($args['token']),true);
            $data_log['user_id'] = $user['user_id']??'';
        }
        //返回结果
        $data_log['status'] = $status?:1;

        $data_log['result'] = json_encode($result, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_NUMERIC_CHECK);
        if(strlen($data_log['result']) > 60000) {
            $data_log['result'] = '数据太多，不记录';
        }

        //保存
        $request_id = Db::name('api_log')->insertGetId($data_log);
    }

    public function result_json($return){
        if(input('needinit')){
            $menu_data = Db::name('designer_menu')->where('1=1')->value('menudata');
            $menu_data = json_decode($menu_data,true);
            $return['_initdata']['menudata'] = $menu_data;
            $return['_initdata']['color1'] = tpCache('basic.color1');
            $return['_initdata']['color2'] = tpCache('basic.color2');
            $return['_initdata']['color1rgb'] = hex2rgb(tpCache('basic.color1'));
            $return['_initdata']['color2rgb'] = hex2rgb(tpCache('basic.color2'));
            $domain_url = getCurUrl();
            if(strpos($domain_url,'https://')!==false){
                $wssurl = str_replace('https://', 'wss://', $domain_url).'/wss';
            }else{
                $wssurl = str_replace('http://', 'wss://', $domain_url).'/wss';
            }
//            $wssurl = 'ws://127.0.0.1:7272';
            $return['_initdata']['wssurl'] = $wssurl;
        }
        $user_id = 0;
        if(input('token')){
            $return['token'] = input('token');
            $user = json_decode(base64_decode(input('token')),true);

            if(!empty($user['user_id'])){
                $user_id = $user['user_id'];
            }
        }
        $return['user_id'] = $user_id;
        if(isset($return['data']['pagecontent'])){
            $return['data']['pagecontent'] = json_decode($return['data']['pagecontent'],true);
        }
        writeLog('返回内容2'.json_encode($return),'filesync');
        return json($return);
    }
}


